<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>内存结构和OOM | 自由·生活</title>
    <meta name="generator" content="VuePress 1.5.4">
    <link rel="icon" href="images/favicon.ico">
    <script language="javascript" type="text/javascript" src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script language="javascript" type="text/javascript" src="/js/MouseClickEffect.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.11.1/katex.min.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/github-markdown-css/4.0.0/github-markdown.min.css">
    <meta name="description" content="脱离生存而生活，寻求自由">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
    <link rel="preload" href="/assets/css/0.styles.8c3033ea.css" as="style"><link rel="preload" href="/assets/js/app.0e76bb72.js" as="script"><link rel="preload" href="/assets/js/3.c5829877.js" as="script"><link rel="preload" href="/assets/js/1.695f3f80.js" as="script"><link rel="preload" href="/assets/js/11.5d72653a.js" as="script"><link rel="prefetch" href="/assets/js/10.fdc11bc7.js"><link rel="prefetch" href="/assets/js/100.3e29fa54.js"><link rel="prefetch" href="/assets/js/101.19f861f4.js"><link rel="prefetch" href="/assets/js/102.0eb344c2.js"><link rel="prefetch" href="/assets/js/103.167b5aab.js"><link rel="prefetch" href="/assets/js/104.3463b648.js"><link rel="prefetch" href="/assets/js/105.fc782f3d.js"><link rel="prefetch" href="/assets/js/106.b2d60f6a.js"><link rel="prefetch" href="/assets/js/107.91f5548b.js"><link rel="prefetch" href="/assets/js/108.8bcbfcd7.js"><link rel="prefetch" href="/assets/js/109.a0dd87a3.js"><link rel="prefetch" href="/assets/js/110.8d3b5064.js"><link rel="prefetch" href="/assets/js/111.6e109181.js"><link rel="prefetch" href="/assets/js/112.7130a6e7.js"><link rel="prefetch" href="/assets/js/113.6c962a0e.js"><link rel="prefetch" href="/assets/js/114.82175aae.js"><link rel="prefetch" href="/assets/js/115.a83e6a2d.js"><link rel="prefetch" href="/assets/js/116.70ebc8b4.js"><link rel="prefetch" href="/assets/js/117.368509d0.js"><link rel="prefetch" href="/assets/js/118.54fdc470.js"><link rel="prefetch" href="/assets/js/119.64d2a2a3.js"><link rel="prefetch" href="/assets/js/12.bfde6b78.js"><link rel="prefetch" href="/assets/js/120.5b47488f.js"><link rel="prefetch" href="/assets/js/121.82865d4b.js"><link rel="prefetch" href="/assets/js/122.619e60ca.js"><link rel="prefetch" href="/assets/js/123.66c3fb26.js"><link rel="prefetch" href="/assets/js/124.4381768b.js"><link rel="prefetch" href="/assets/js/125.2f39619b.js"><link rel="prefetch" href="/assets/js/126.0c622bc6.js"><link rel="prefetch" href="/assets/js/127.caee15ee.js"><link rel="prefetch" href="/assets/js/128.99714900.js"><link rel="prefetch" href="/assets/js/129.ff59083b.js"><link rel="prefetch" href="/assets/js/13.2b3528b1.js"><link rel="prefetch" href="/assets/js/130.76df0e89.js"><link rel="prefetch" href="/assets/js/131.2333a328.js"><link rel="prefetch" href="/assets/js/132.65cff4f4.js"><link rel="prefetch" href="/assets/js/133.2c0991bc.js"><link rel="prefetch" href="/assets/js/134.fe5fc3a5.js"><link rel="prefetch" href="/assets/js/135.aef0a809.js"><link rel="prefetch" href="/assets/js/136.90fd1b1d.js"><link rel="prefetch" href="/assets/js/137.07750a3f.js"><link rel="prefetch" href="/assets/js/138.723bd2c0.js"><link rel="prefetch" href="/assets/js/139.d22450b7.js"><link rel="prefetch" href="/assets/js/14.b769124c.js"><link rel="prefetch" href="/assets/js/140.1659de26.js"><link rel="prefetch" href="/assets/js/141.f713c5ec.js"><link rel="prefetch" href="/assets/js/142.63ebd83f.js"><link rel="prefetch" href="/assets/js/143.404a833f.js"><link rel="prefetch" href="/assets/js/144.3e0107ef.js"><link rel="prefetch" href="/assets/js/145.cef80531.js"><link rel="prefetch" href="/assets/js/146.c68d17d7.js"><link rel="prefetch" href="/assets/js/147.0af3acbb.js"><link rel="prefetch" href="/assets/js/148.9f0d2452.js"><link rel="prefetch" href="/assets/js/149.28785960.js"><link rel="prefetch" href="/assets/js/15.7c3421d9.js"><link rel="prefetch" href="/assets/js/150.bf310c33.js"><link rel="prefetch" href="/assets/js/151.de6313cc.js"><link rel="prefetch" href="/assets/js/152.91a567b5.js"><link rel="prefetch" href="/assets/js/153.cfe26382.js"><link rel="prefetch" href="/assets/js/154.047349d0.js"><link rel="prefetch" href="/assets/js/155.6923503c.js"><link rel="prefetch" href="/assets/js/156.2443e22f.js"><link rel="prefetch" href="/assets/js/157.fac2fc32.js"><link rel="prefetch" href="/assets/js/158.3d470963.js"><link rel="prefetch" href="/assets/js/159.9512719e.js"><link rel="prefetch" href="/assets/js/16.baa49355.js"><link rel="prefetch" href="/assets/js/160.1a610cc3.js"><link rel="prefetch" href="/assets/js/161.b240977c.js"><link rel="prefetch" href="/assets/js/162.cfb78d5a.js"><link rel="prefetch" href="/assets/js/163.3c935aea.js"><link rel="prefetch" href="/assets/js/164.0188664b.js"><link rel="prefetch" href="/assets/js/165.6f4ba8e0.js"><link rel="prefetch" href="/assets/js/166.32b92de7.js"><link rel="prefetch" href="/assets/js/167.1087cc58.js"><link rel="prefetch" href="/assets/js/168.7e81ac69.js"><link rel="prefetch" href="/assets/js/169.5270c30b.js"><link rel="prefetch" href="/assets/js/17.2270e174.js"><link rel="prefetch" href="/assets/js/170.1d6bc941.js"><link rel="prefetch" href="/assets/js/171.e3781bc6.js"><link rel="prefetch" href="/assets/js/172.54d7bc4b.js"><link rel="prefetch" href="/assets/js/173.a88f9e1f.js"><link rel="prefetch" href="/assets/js/174.7d882351.js"><link rel="prefetch" href="/assets/js/175.7a0c6feb.js"><link rel="prefetch" href="/assets/js/176.12b22e86.js"><link rel="prefetch" href="/assets/js/177.b2230eac.js"><link rel="prefetch" href="/assets/js/178.023ba5be.js"><link rel="prefetch" href="/assets/js/179.53fa03a8.js"><link rel="prefetch" href="/assets/js/18.988151ff.js"><link rel="prefetch" href="/assets/js/180.f17c3614.js"><link rel="prefetch" href="/assets/js/181.8872481a.js"><link rel="prefetch" href="/assets/js/182.c57e38bc.js"><link rel="prefetch" href="/assets/js/183.7085d12e.js"><link rel="prefetch" href="/assets/js/184.aa164e5b.js"><link rel="prefetch" href="/assets/js/185.b7ed0f6b.js"><link rel="prefetch" href="/assets/js/186.f08304c9.js"><link rel="prefetch" href="/assets/js/187.4a10e63d.js"><link rel="prefetch" href="/assets/js/188.f0463437.js"><link rel="prefetch" href="/assets/js/189.cff969e0.js"><link rel="prefetch" href="/assets/js/19.c196a261.js"><link rel="prefetch" href="/assets/js/190.adc3524b.js"><link rel="prefetch" href="/assets/js/191.a14895a8.js"><link rel="prefetch" href="/assets/js/192.e595f819.js"><link rel="prefetch" href="/assets/js/193.3cbcef2c.js"><link rel="prefetch" href="/assets/js/194.55530a96.js"><link rel="prefetch" href="/assets/js/195.1a834f79.js"><link rel="prefetch" href="/assets/js/196.001dd10b.js"><link rel="prefetch" href="/assets/js/197.4b9de142.js"><link rel="prefetch" href="/assets/js/198.955a06ea.js"><link rel="prefetch" href="/assets/js/199.9d63eea7.js"><link rel="prefetch" href="/assets/js/20.74b35545.js"><link rel="prefetch" href="/assets/js/200.8fe0777d.js"><link rel="prefetch" href="/assets/js/201.a91425c3.js"><link rel="prefetch" href="/assets/js/202.5b89464a.js"><link rel="prefetch" href="/assets/js/203.e48d18e2.js"><link rel="prefetch" href="/assets/js/204.6cbc5c79.js"><link rel="prefetch" href="/assets/js/205.d26879e9.js"><link rel="prefetch" href="/assets/js/206.fd8688f3.js"><link rel="prefetch" href="/assets/js/207.ef25eb3d.js"><link rel="prefetch" href="/assets/js/208.9f0e5ab2.js"><link rel="prefetch" href="/assets/js/209.cbd7c763.js"><link rel="prefetch" href="/assets/js/21.3cbc9110.js"><link rel="prefetch" href="/assets/js/210.6b733c20.js"><link rel="prefetch" href="/assets/js/211.2b986a0a.js"><link rel="prefetch" href="/assets/js/212.faee0dc6.js"><link rel="prefetch" href="/assets/js/213.7ed75653.js"><link rel="prefetch" href="/assets/js/214.825d2d93.js"><link rel="prefetch" href="/assets/js/215.251aa760.js"><link rel="prefetch" href="/assets/js/216.11962b03.js"><link rel="prefetch" href="/assets/js/217.62e3335a.js"><link rel="prefetch" href="/assets/js/218.a7c3b793.js"><link rel="prefetch" href="/assets/js/219.5ff63a4c.js"><link rel="prefetch" href="/assets/js/22.d2507cce.js"><link rel="prefetch" href="/assets/js/220.28ea20d6.js"><link rel="prefetch" href="/assets/js/221.6108e4e1.js"><link rel="prefetch" href="/assets/js/222.73443d85.js"><link rel="prefetch" href="/assets/js/223.e115734d.js"><link rel="prefetch" href="/assets/js/224.499e0bab.js"><link rel="prefetch" href="/assets/js/225.70887ebb.js"><link rel="prefetch" href="/assets/js/226.e683c623.js"><link rel="prefetch" href="/assets/js/227.06f8bc32.js"><link rel="prefetch" href="/assets/js/228.82faf31a.js"><link rel="prefetch" href="/assets/js/229.0f8a6911.js"><link rel="prefetch" href="/assets/js/23.8da30b89.js"><link rel="prefetch" href="/assets/js/230.f895ca67.js"><link rel="prefetch" href="/assets/js/231.34717d1d.js"><link rel="prefetch" href="/assets/js/232.c7857239.js"><link rel="prefetch" href="/assets/js/233.6b7bcffd.js"><link rel="prefetch" href="/assets/js/234.0e0da6a5.js"><link rel="prefetch" href="/assets/js/235.5bc065c0.js"><link rel="prefetch" href="/assets/js/236.3e91ceb0.js"><link rel="prefetch" href="/assets/js/237.9116740e.js"><link rel="prefetch" href="/assets/js/238.8c792409.js"><link rel="prefetch" href="/assets/js/239.b4d8dfb8.js"><link rel="prefetch" href="/assets/js/24.3014a3d4.js"><link rel="prefetch" href="/assets/js/240.a1e61d58.js"><link rel="prefetch" href="/assets/js/241.3c016709.js"><link rel="prefetch" href="/assets/js/242.f8620e3f.js"><link rel="prefetch" href="/assets/js/243.8f53f134.js"><link rel="prefetch" href="/assets/js/244.424903c4.js"><link rel="prefetch" href="/assets/js/245.96c86aae.js"><link rel="prefetch" href="/assets/js/25.03ca3aa9.js"><link rel="prefetch" href="/assets/js/26.4916e6b8.js"><link rel="prefetch" href="/assets/js/27.f9aab712.js"><link rel="prefetch" href="/assets/js/28.1dfbff97.js"><link rel="prefetch" href="/assets/js/29.f7a69544.js"><link rel="prefetch" href="/assets/js/30.e3062801.js"><link rel="prefetch" href="/assets/js/31.a510760d.js"><link rel="prefetch" href="/assets/js/32.7d281288.js"><link rel="prefetch" href="/assets/js/33.6ee8ebed.js"><link rel="prefetch" href="/assets/js/34.b32e8d79.js"><link rel="prefetch" href="/assets/js/35.59d10d28.js"><link rel="prefetch" href="/assets/js/36.e1bcc1a4.js"><link rel="prefetch" href="/assets/js/37.f6c202dd.js"><link rel="prefetch" href="/assets/js/38.f8d5161a.js"><link rel="prefetch" href="/assets/js/39.1b4b64a0.js"><link rel="prefetch" href="/assets/js/4.77ea1544.js"><link rel="prefetch" href="/assets/js/40.efb0ab3f.js"><link rel="prefetch" href="/assets/js/41.85c7d269.js"><link rel="prefetch" href="/assets/js/42.36b0df70.js"><link rel="prefetch" href="/assets/js/43.c3cc85ee.js"><link rel="prefetch" href="/assets/js/44.b1575b98.js"><link rel="prefetch" href="/assets/js/45.8de97fe3.js"><link rel="prefetch" href="/assets/js/46.1fe43d4f.js"><link rel="prefetch" href="/assets/js/47.70a2e6cc.js"><link rel="prefetch" href="/assets/js/48.bb81e90c.js"><link rel="prefetch" href="/assets/js/49.8f4841fa.js"><link rel="prefetch" href="/assets/js/5.b3344219.js"><link rel="prefetch" href="/assets/js/50.e7216bda.js"><link rel="prefetch" href="/assets/js/51.1a2faa73.js"><link rel="prefetch" href="/assets/js/52.f625df74.js"><link rel="prefetch" href="/assets/js/53.b0067c47.js"><link rel="prefetch" href="/assets/js/54.0c85623e.js"><link rel="prefetch" href="/assets/js/55.491eeca5.js"><link rel="prefetch" href="/assets/js/56.8c9ffe2c.js"><link rel="prefetch" href="/assets/js/57.51902d8c.js"><link rel="prefetch" href="/assets/js/58.6d40a504.js"><link rel="prefetch" href="/assets/js/59.dbb8914b.js"><link rel="prefetch" href="/assets/js/6.a7455cf0.js"><link rel="prefetch" href="/assets/js/60.99f5caf1.js"><link rel="prefetch" href="/assets/js/61.ad9ebd9b.js"><link rel="prefetch" href="/assets/js/62.8c2c2783.js"><link rel="prefetch" href="/assets/js/63.2334b133.js"><link rel="prefetch" href="/assets/js/64.c79651bc.js"><link rel="prefetch" href="/assets/js/65.791edb2c.js"><link rel="prefetch" href="/assets/js/66.bd09db30.js"><link rel="prefetch" href="/assets/js/67.1311433b.js"><link rel="prefetch" href="/assets/js/68.8be881a0.js"><link rel="prefetch" href="/assets/js/69.f9bb1936.js"><link rel="prefetch" href="/assets/js/7.274fc010.js"><link rel="prefetch" href="/assets/js/70.77e0e02e.js"><link rel="prefetch" href="/assets/js/71.da2cc493.js"><link rel="prefetch" href="/assets/js/72.f4a80206.js"><link rel="prefetch" href="/assets/js/73.8d4c37c8.js"><link rel="prefetch" href="/assets/js/74.7a7304d5.js"><link rel="prefetch" href="/assets/js/75.2e6d01bf.js"><link rel="prefetch" href="/assets/js/76.7de509a7.js"><link rel="prefetch" href="/assets/js/77.e5e58459.js"><link rel="prefetch" href="/assets/js/78.1590a03f.js"><link rel="prefetch" href="/assets/js/79.a96442ae.js"><link rel="prefetch" href="/assets/js/8.26184cc0.js"><link rel="prefetch" href="/assets/js/80.76d4faa5.js"><link rel="prefetch" href="/assets/js/81.c79d5483.js"><link rel="prefetch" href="/assets/js/82.47926428.js"><link rel="prefetch" href="/assets/js/83.ec12a53e.js"><link rel="prefetch" href="/assets/js/84.72120681.js"><link rel="prefetch" href="/assets/js/85.30643e8b.js"><link rel="prefetch" href="/assets/js/86.64375c3f.js"><link rel="prefetch" href="/assets/js/87.7a86109f.js"><link rel="prefetch" href="/assets/js/88.68858482.js"><link rel="prefetch" href="/assets/js/89.00d57e24.js"><link rel="prefetch" href="/assets/js/9.56db4e9b.js"><link rel="prefetch" href="/assets/js/90.b7f9de16.js"><link rel="prefetch" href="/assets/js/91.e9a8e6fb.js"><link rel="prefetch" href="/assets/js/92.0b986faf.js"><link rel="prefetch" href="/assets/js/93.7c26025a.js"><link rel="prefetch" href="/assets/js/94.c39f793d.js"><link rel="prefetch" href="/assets/js/95.b74865a0.js"><link rel="prefetch" href="/assets/js/96.62558dca.js"><link rel="prefetch" href="/assets/js/97.945f9035.js"><link rel="prefetch" href="/assets/js/98.be789959.js"><link rel="prefetch" href="/assets/js/99.1adf100e.js">
    <link rel="stylesheet" href="/assets/css/0.styles.8c3033ea.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container" data-v-cd01cc30><div data-v-cd01cc30><div id="loader-wrapper" class="loading-wrapper" data-v-4cd7a24a data-v-cd01cc30 data-v-cd01cc30><div class="loader-main" data-v-4cd7a24a><div data-v-4cd7a24a></div><div data-v-4cd7a24a></div><div data-v-4cd7a24a></div><div data-v-4cd7a24a></div></div> <!----> <!----></div> <div class="password-shadow password-wrapper-out" style="display:none;" data-v-623c7d26 data-v-cd01cc30 data-v-cd01cc30><h3 class="title" style="display:none;" data-v-623c7d26 data-v-623c7d26>自由·生活</h3> <!----> <label id="box" class="inputBox" style="display:none;" data-v-623c7d26 data-v-623c7d26><input type="password" value="" data-v-623c7d26> <span data-v-623c7d26>Konck! Knock!</span> <button data-v-623c7d26>OK</button></label> <div class="footer" style="display:none;" data-v-623c7d26 data-v-623c7d26><span data-v-623c7d26><i class="iconfont reco-theme" data-v-623c7d26></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-623c7d26>vuePress-theme-reco</a></span> <span data-v-623c7d26><i class="iconfont reco-copyright" data-v-623c7d26></i> <a data-v-623c7d26><span data-v-623c7d26>丶忆yu'man</span>
            
          <span data-v-623c7d26>2020 - </span>
          2021
        </a></span></div></div> <div class="hide" data-v-cd01cc30><header class="navbar" data-v-cd01cc30><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/images/avatar.jpg" alt="自由·生活" class="logo"> <span class="site-name">自由·生活</span></a> <div class="links"><div class="color-picker"><a class="color-button"><i class="iconfont reco-color"></i></a> <div class="color-picker-menu" style="display:none;"><div class="mode-options"><h4 class="title">Choose mode</h4> <ul class="color-mode-options"><li class="dark">dark</li><li class="auto active">auto</li><li class="light">light</li></ul></div></div></div> <div class="search-box"><i class="iconfont reco-search"></i> <input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link"><i class="iconfont reco-home"></i>
  主页
</a></div><div class="nav-item"><a href="/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  时间轴
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/前端/" class="nav-link"><i class="iconfont undefined"></i>
  前端
</a></li><li class="dropdown-item"><!----> <a href="/categories/ML&amp;DL/" class="nav-link"><i class="iconfont undefined"></i>
  ML&amp;DL
</a></li><li class="dropdown-item"><!----> <a href="/categories/力扣仓库/" class="nav-link"><i class="iconfont undefined"></i>
  力扣仓库
</a></li><li class="dropdown-item"><!----> <a href="/categories/后端/" class="nav-link"><i class="iconfont undefined"></i>
  后端
</a></li><li class="dropdown-item"><!----> <a href="/categories/开发工具/" class="nav-link"><i class="iconfont undefined"></i>
  开发工具
</a></li><li class="dropdown-item"><!----> <a href="/categories/学习笔记/" class="nav-link"><i class="iconfont undefined"></i>
  学习笔记
</a></li><li class="dropdown-item"><!----> <a href="/categories/影视/" class="nav-link"><i class="iconfont undefined"></i>
  影视
</a></li><li class="dropdown-item"><!----> <a href="/categories/码农生涯/" class="nav-link"><i class="iconfont undefined"></i>
  码农生涯
</a></li><li class="dropdown-item"><!----> <a href="/categories/生活随笔/" class="nav-link"><i class="iconfont undefined"></i>
  生活随笔
</a></li></ul></div></div><div class="nav-item"><a href="/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-document"></i>
      教程
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/docs/course/blog/" class="nav-link"><i class="iconfont undefined"></i>
  博客系列教程
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-menu"></i>
      关联平台
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://github.com/zhiyi-He" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  GitHub
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://gitee.com/zhiyi-He" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-mayun"></i>
  Gitee
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul></div></div><div class="nav-item"><a href="/docs/about/" class="nav-link"><i class="iconfont reco-account"></i>
  关于我
</a></div> <!----></nav></div></header> <div class="sidebar-mask" data-v-cd01cc30></div> <aside class="sidebar" data-v-cd01cc30><div class="personal-info-wrapper" data-v-2a0ed2ae data-v-cd01cc30><img src="/images/avatar.jpg" alt="author-avatar" class="personal-img" data-v-2a0ed2ae> <h3 class="name" data-v-2a0ed2ae>
    丶忆yu'man
  </h3> <div class="num" data-v-2a0ed2ae><div data-v-2a0ed2ae><h3 data-v-2a0ed2ae>217</h3> <h6 data-v-2a0ed2ae>文章</h6></div> <div data-v-2a0ed2ae><h3 data-v-2a0ed2ae>15</h3> <h6 data-v-2a0ed2ae>标签</h6></div></div> <hr data-v-2a0ed2ae></div> <nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link"><i class="iconfont reco-home"></i>
  主页
</a></div><div class="nav-item"><a href="/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  时间轴
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/前端/" class="nav-link"><i class="iconfont undefined"></i>
  前端
</a></li><li class="dropdown-item"><!----> <a href="/categories/ML&amp;DL/" class="nav-link"><i class="iconfont undefined"></i>
  ML&amp;DL
</a></li><li class="dropdown-item"><!----> <a href="/categories/力扣仓库/" class="nav-link"><i class="iconfont undefined"></i>
  力扣仓库
</a></li><li class="dropdown-item"><!----> <a href="/categories/后端/" class="nav-link"><i class="iconfont undefined"></i>
  后端
</a></li><li class="dropdown-item"><!----> <a href="/categories/开发工具/" class="nav-link"><i class="iconfont undefined"></i>
  开发工具
</a></li><li class="dropdown-item"><!----> <a href="/categories/学习笔记/" class="nav-link"><i class="iconfont undefined"></i>
  学习笔记
</a></li><li class="dropdown-item"><!----> <a href="/categories/影视/" class="nav-link"><i class="iconfont undefined"></i>
  影视
</a></li><li class="dropdown-item"><!----> <a href="/categories/码农生涯/" class="nav-link"><i class="iconfont undefined"></i>
  码农生涯
</a></li><li class="dropdown-item"><!----> <a href="/categories/生活随笔/" class="nav-link"><i class="iconfont undefined"></i>
  生活随笔
</a></li></ul></div></div><div class="nav-item"><a href="/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-document"></i>
      教程
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/docs/course/blog/" class="nav-link"><i class="iconfont undefined"></i>
  博客系列教程
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-menu"></i>
      关联平台
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://github.com/zhiyi-He" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  GitHub
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li><li class="dropdown-item"><!----> <a href="https://gitee.com/zhiyi-He" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-mayun"></i>
  Gitee
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul></div></div><div class="nav-item"><a href="/docs/about/" class="nav-link"><i class="iconfont reco-account"></i>
  关于我
</a></div> <!----></nav> <ul class="sidebar-links"><li><a href="/blogs/%E5%90%8E%E7%AB%AF/" aria-current="page" class="sidebar-link">前言</a></li><li><a href="/blogs/%E5%90%8E%E7%AB%AF/basic/" aria-current="page" class="sidebar-link">基础</a></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>java基础</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>java虚拟机</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/blogs/后端/basic/jvm/memory-structure.html" class="active sidebar-link">内存结构和OOM</a></li><li><a href="/blogs/后端/basic/jvm/GC-and-memory-allocation.html" class="sidebar-link">垃圾回收和内存分配</a></li></ul></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>多线程和并发</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>MySQL</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Redis</span> <span class="arrow right"></span></p> <!----></section></li><li><a href="/blogs/%E5%90%8E%E7%AB%AF/framework/" class="sidebar-link">应用框架</a></li></ul> </aside> <div class="password-shadow password-wrapper-in" style="display:none;" data-v-623c7d26 data-v-cd01cc30><h3 class="title" style="display:none;" data-v-623c7d26 data-v-623c7d26>内存结构和OOM</h3> <!----> <label id="box" class="inputBox" style="display:none;" data-v-623c7d26 data-v-623c7d26><input type="password" value="" data-v-623c7d26> <span data-v-623c7d26>Konck! Knock!</span> <button data-v-623c7d26>OK</button></label> <div class="footer" style="display:none;" data-v-623c7d26 data-v-623c7d26><span data-v-623c7d26><i class="iconfont reco-theme" data-v-623c7d26></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-623c7d26>vuePress-theme-reco</a></span> <span data-v-623c7d26><i class="iconfont reco-copyright" data-v-623c7d26></i> <a data-v-623c7d26><span data-v-623c7d26>丶忆yu'man</span>
            
          <span data-v-623c7d26>2020 - </span>
          2021
        </a></span></div></div> <div data-v-cd01cc30><main class="page"><div class="page-title" style="display:none;"><h1 class="title">内存结构和OOM</h1> <div data-v-8bc6ae5c><i class="iconfont reco-account" data-v-8bc6ae5c><span data-v-8bc6ae5c>丶忆yu'man</span></i> <i class="iconfont reco-date" data-v-8bc6ae5c><span data-v-8bc6ae5c>2020-11-18</span></i> <i class="iconfont reco-eye" data-v-8bc6ae5c><span id="/blogs/%E5%90%8E%E7%AB%AF/basic/jvm/memory-structure.html" data-flag-title="Your Article Title" class="leancloud-visitors" data-v-8bc6ae5c><a class="leancloud-visitors-count" style="font-size:.9rem;font-weight:normal;color:#999;"></a></span></i> <i class="iconfont reco-tag tags" data-v-8bc6ae5c><span class="tag-item" data-v-8bc6ae5c>java</span></i></div></div> <div class="theme-reco-content content__default" style="display:none;"><div class="custom-block tip"><p>Java与C++之间有一堵由内存动态分配和垃圾收集技术所围成的“高墙”，墙外面的人想进去，墙里面的人却想出来。</p></div> <h1 id="内存结构和oom">内存结构和OOM</h1> <h2 id="内存结构">内存结构</h2> <p><strong>Java内存管理与C++的不同：</strong></p> <ol><li>Java能够实现自动内存管理，不需要去手动分配内存、回收内存，只需要new个对象就可以了。</li> <li>Java不容易出现内存泄漏和内存溢出问题。</li> <li>内存管理完全交给虚拟机操作，因此出现问题时无法及时解决，而C++都是手动管理。</li></ol> <h3 id="运行时数据区">运行时数据区</h3> <p>Java虚拟机在执行Java程序的过程中会把它所管理的内存划分为若干个不同的数据区域，整个数据区域被称为运行时数据区，其中有的区域随着虚拟机进程的启动而存在，有些区域则依赖用户线程的启动和结束而建立和销毁。前一个区域叫做共享数据区，后一个区域叫私有数据区，在共享数据区中包括堆和方法区，而在私有数据区中包括程序计数器、虚拟机栈和本地方法栈三个部分。下面是运行时数据区的结构图。</p> <p><img src="/assets/img/image-20201118231232017.648c6c95.png" alt="image-20201118231232017"></p> <h3 id="共享数据区">共享数据区</h3> <ul><li><p>堆（Heap）</p> <p><strong>作用：</strong> 唯一目的就是存放对象实例，几乎所有的对象实例和数组都在堆中分配内存。</p> <p><strong>区域分配：</strong> 垃圾回收主要是针对内存进行回收，因此Java堆是垃圾收集器管理的主要区域。Java堆从<strong>内存回收</strong>来看，主要分为<strong>新生代</strong>、<strong>老年代</strong>，其中新生代往下细分的话分为Eden空间、From Survivor空间、To Survivor空间等；从<strong>内存分配</strong>来看，线程共享的Java堆中可能划分出线程私有的分配缓冲区（<strong>TLAB</strong>）。无论如何划分，内部存储的仍然是对象实例，<strong>划分的目的</strong>都是为了更好地回收内存、更快地分配内存。</p> <p><strong>存储特点：</strong> Java堆物理存储可以在不连续的空间上，只需要在逻辑上连续的空间。</p></li> <li><p>方法区</p> <p><strong>作用：</strong> 存储<strong>已被虚拟机加载</strong>的类信息、常量、静态变量、即时编译器编译后的代码等数据。</p> <p><strong>描述：</strong> 方法区也存在另一个名称——<strong>永久代</strong>，这就与Java堆中新生代、老年代相对应，这不是说其中的内存不进行垃圾回收，而是其中的数据信息很少进行GC操作，垃圾回收的要求比较严格。在方法区的垃圾回收目标主要是<strong>常量池的回收以及对类型的卸载</strong>。</p> <p><strong>运行时常量池：</strong> 用于存放<strong>编译期</strong>生成的各种字面量和符号引用，例如字符串不可变常量存储在常量池中，而字符串对象存储在堆中。运行时常量池具备<strong>动态性</strong>，也就是说不一定只能存储编译期生成的常量，<strong>运行期也可以放入常量到常量池中</strong>，例如String类的intern()方法，该方法会先查看常量池是否存在当前字符串，如果有的话直接返回字符串引用，如果没有则将它加入到常量池中。</p></li></ul> <h3 id="私有数据区">私有数据区</h3> <ul><li><p>程序计数器</p> <p>程序计数器可以理解为当前线程字节码的<strong>行号指示器</strong>，程序的执行就依赖于程序计数器改变值选取下一条执行语句。分支、循环、跳转等基础功能都需要这个计数器才能完成。</p> <p>由于Java虚拟机多线程是通过给线程分配CPU时间片来执行程序的，因此每个线程只能执行一个时间片的时间，因此单个线程的程序执行必须中断，每个线程必须要有一个程序计数器来保存执行位置信息，这样才能保证线程能够恢复到原来的执行位置。</p> <p>每条线程都需要有一个独立的程序计数器，各条线程之间计数器<strong>互不影响，独立存储</strong>。</p></li> <li><p>虚拟机栈</p> <p><strong>作用：</strong> 描述Java方法执行的内存模型，创建一个<strong>栈帧</strong>用于存储局部变量表、操作数栈、动态链接、方法出口等信息。</p> <p><strong>方法执行的过程：</strong> 每个方法在执行的时候都会创建一个栈帧，这个栈帧存储着方法执行需要的信息，栈帧也可以理解为一个栈的起始地址，<strong>在方法执行时，栈帧入栈，方法执行完成后，栈帧出栈。</strong></p> <p><strong>局部变量表</strong>存放着各种基本数据类型、引用类型以及returnAddress类型(指向一条字节码指令的地址，程序计数器的作用)。<strong>局部变量表的大小在方法执行期间是固定的</strong>，由多个局部变量空间构成，其中每个局部变量空间的大小为4个字节。</p></li> <li><p>本地方法栈</p> <p><strong>作用：</strong> 描述Java<strong>本地方法</strong>执行的内存模型，而虚拟机栈应该理解为程序员<strong>自定义的方法</strong>。</p> <p>本地方法栈的结构和运行机制类似与虚拟机栈，其实两者只是方法服务对象不一样，而其它内容都差不多。</p></li></ul> <h3 id="直接内存">直接内存</h3> <p><strong>描述：</strong> 直接内存并不是运行时数据区中的一部分，而是一种被分配的<strong>本地内存空间</strong>，通过存储在Java堆中的<strong>DirectByteBuffer对象</strong>作为该内存的引用进行操作。在虚拟机与外界通讯时，通常采用Native堆来作为缓存使用，有效提高系统性能。</p> <p>直接内存因为不是Java堆的一部分，因此<strong>内存空间不受虚拟机限制</strong>，而是受本机总内存大小以及处理器寻址空间的限制。</p> <h2 id="对象">对象</h2> <p>详细介绍一下对象在Java堆中内存分配，对象结构以及对象访问的全过程，对Java堆的理解会更加深刻。</p> <h3 id="对象创建">对象创建</h3> <p>对象创建的过程分为如下几步：</p> <ol><li><p>类加载</p> <p>虚拟机遇到一条new指令时，首先将去<strong>检查</strong>这个指令的参数是否能在常量池中定位到一个类的符号引用，并且<strong>检查</strong>这个符号引用代表的类是否已被加载、解析和初始化过。如果没有，那必须先执行相应的<strong>类加载过程。</strong></p></li> <li><p>内存分配</p> <p>为新生对象分配内存空间，对象所需内存的大小在类加载完成后便可<strong>完全确定</strong>。按照Java堆中内存是否连续划分，可分为<strong>指针碰撞</strong>（Bump the Pointer）和<strong>空闲列表</strong>（Free List）两种分配方式。</p> <ul><li><p>指针碰撞</p> <p>Java堆中<strong>内存是规整的</strong>，使用过的内存和未使用的内存分别在一边，中间有一个<strong>指针</strong>来分隔两边空间，为新生对象分配内存时，只需要将这个指针向空闲空间方向移动一段对象内存。</p> <p>垃圾收集算法采用<strong>标记-整理</strong>（Mark-Compact）时，内存分配采用指针碰撞方式。例如Serial、ParNew、Serial Old、Parallel Old等垃圾收集器。</p></li> <li><p>空闲列表</p> <p>Java堆中内存是断续的，<strong>已使用的内存和空间内存相互交错</strong>，因此无法仅靠指针移动分配对象内存，因此虚拟机<strong>维护一个内存列表</strong>，记录着哪些内存块是可用的，为新生对象分配内存时，在列表中寻找一块大于对象内存的空间划分给对象实例，并更新列表记录。</p> <p>垃圾收集算法采用<strong>标记-清除</strong>（Mark-Sweep）时，内存分配采用空闲列表方式。例如CMS垃圾收集器。</p></li></ul> <p><strong>在并发情况下内存分配是不安全的</strong>，在A对象分配内存时，另一线程的B对象有可能也在这块内存分配内存。</p> <p><strong>Java虚拟机的解决方式：</strong> 之前谈论到Java堆中会分配出线程私有的<strong>TLAB</strong>空间，也称为本地线程分配缓冲，线程分配对象内存时，<strong>先在TLAB空间上分配</strong>，当TLAB空间用完之后，对线程进行<strong>同步锁定</strong>来重新分配TLAB空间。这也是两种并发分配内存解决方案的综合考虑。</p></li> <li><p>初始化</p> <p>将分配到的对象内存空间初始化为<strong>对应数据类型的零值</strong>，如果使用TLAB，可以在Java堆分配TLAB空间时就初始化。</p> <p>初始化过程保证了Java对象实例<strong>无需赋初始值也可以使用</strong>，例如Integer对象默认为0。</p></li> <li><p>对象头设置</p> <p><strong>虚拟机对对象进行某些必要的设置</strong>，例如这个对象是哪个类的实例、如何才能找到类的元数据信息、对象的哈希码、对象的GC分代年龄等信息，并将这个信息存放在对象头中。</p></li> <li><p>执行init方法</p> <p><strong>对虚拟机而言</strong>，对象在上一步已经被创建了，<strong>对程序员而言</strong>，对象还没有赋予初始值，也就是没有执行init方法，<strong>执行完new指令就开始执行init方法</strong>。例如为一个Integer对象初始化：Integer a = 3。</p></li></ol> <h3 id="对象存储">对象存储</h3> <p>对象在内存中存储的布局分为<strong>对象头</strong>（Header）、<strong>实例数据</strong>（Instance Data）和<strong>对齐填充</strong>（Padding）三个区域。</p> <ul><li><p>对象头</p> <p>对象头包括两部分信息，第一部分用来存储对象自身的运行时数据，也称为<strong>Mark Word</strong>。</p> <p>Mark Word被设计成一个<strong>非固定的数据结构</strong>以便在极小的空间内存储尽量多的信息，它会根据对象的状态复用自己的存储空间。也就是说其中存储内容所占bit会根据状态发生变化。</p> <p>第二部分是<strong>类型指针</strong>，即对象指向它的类元数据的指针，虚拟机通过这个指针来确定这个对象是哪个类的实例。<strong>查找对象的元数据信息并不一定要经过对象本身</strong>，例如对象访问采用句柄访问的方式，在下文中介绍。</p> <p>在这里我们来理解一下这句话：<strong>如果对象是一个Java数组，那在对象头中还必须有一块用于记录数组长度的数据，因为虚拟机可以通过普通Java对象的元数据信息确定Java对象的大小，但是从数组的元数据中却无法确定数组的大小。</strong></p> <p>这里其实是说数据对象不能通过元数据去确定数据大小，而其它普通的Java对象是可以的，因为<strong>元数据就是描述数据的数据</strong>，也就是类型数据。而数据对象例外。</p></li> <li><p>实例数据</p> <p>实例数据部分是<strong>对象真正存储的有效信息，也是在程序代码中所定义的各种类型的字段内容</strong>。</p> <p>这部分的存储顺序会受到<strong>虚拟机分配策略参数</strong>（FieldsAllocationStyle）和<strong>字段在Java源码中定义顺序</strong>的影响，字段顺序按照类型字节从大到小，例如long、int等，相<strong>同宽度的字段总是被分配到一起</strong>。并且<strong>在父类中定义的变量会出现在子类之前</strong>。</p></li> <li><p>对齐填充</p> <p>对齐填充并没有实际的意义，仅仅起着占位符的作用，主要是因为<strong>对象的大小必须是8字节的整数倍</strong>，因此需要对齐填充来满足要求。</p></li></ul> <h3 id="对象访问">对象访问</h3> <p>目前主流的对象访问方式有<strong>句柄访问</strong>和<strong>直接指针</strong>，Java虚拟机中采用的是直接指针。</p> <ul><li><p>句柄访问</p> <p>Java堆中将会划分出一块内存来作为<strong>句柄池</strong>，reference中存储的就是对象的句柄地址，而<strong>句柄中包含了对象实例数据与类型数据各自的具体地址信息</strong>。</p> <p><img src="/assets/img/image-20201202115212759.66cb79d5.png" alt="image-20201202115212759"></p></li> <li><p>直接指针访问</p> <p>直接指针访问中reference<strong>直接指向对象实例数据</strong>，而<strong>在对象中在存储对象类型数据的地址</strong>。</p> <p><img src="/assets/img/image-20201202114935816.b1fccb5b.png" alt="image-20201202114935816"></p></li></ul> <p><strong>两种访问方式的区别：</strong></p> <ol><li>使用句柄来访问<strong>reference中存储的是稳定的句柄地址</strong>，对象移动时只会改变句柄中的实例数据指针，而reference不用修改。</li> <li>使用直接指针访问<strong>节省了一次指针定位的时间开销，访问速度更快。</strong></li></ol> <h2 id="内存溢出oom">内存溢出OOM</h2> <p>在Java虚拟机规范的描述中，除了程序计数器外，虚拟机内存的其他几个运行时区域都有发生OutOfMemoryError（OOM）异常的可能，下面的内存溢出分类也是按照内存区域划分的。</p> <h3 id="java堆溢出">Java堆溢出</h3> <p><strong>溢出产生：</strong> Java堆主要用来存储对象实例，因此只要<strong>不断创建对象，并且垃圾回收器无法回收这些对象</strong>，在Java堆内存填满后就会产生OOM。</p> <p><strong>异常提示：</strong> <code>java.lang.OutOfMemoryError</code> 会提示 <code>Java heap space</code>。</p> <p><strong>解决方案：</strong></p> <ol><li>通过内存映像分析工具堆Dump出来的堆转储快照进行分析，重点是确认内存中的对象是否是必要的，确定是出现了<strong>内存泄漏</strong>还是<strong>内存溢出</strong>。</li> <li>内存泄漏时，可进一步通过工具查看泄露对象到GC Roots的引用链，找到泄露对象是通过怎样的路径与GC Roots相关联并导致<strong>垃圾收集器无法自动回收它们</strong>的。</li> <li>内存溢出时，内存中所有对象都必须存活，因此<strong>检查堆参数（-Xmx最大值、-Xms最小值）</strong>，调大堆内存。从代码上检查是否存在<strong>某些对象生命周期过长、持有状态时间过长</strong>的情况，尝试减少程序运行期的内存消耗。</li></ol> <h3 id="虚拟机栈和本地方法栈溢出">虚拟机栈和本地方法栈溢出</h3> <p><strong>溢出产生：</strong> 在虚拟机栈和本地方法栈中，存在两种溢出异常情况：</p> <ol><li><p>如果线程请求的栈深度大于虚拟机所允许的最大深度，将抛出<strong>StackOverflowError异常</strong>。</p> <p>这里其实不太好理解，可以先这样认为，Java虚拟机会为每个线程<strong>分配固定大小的内存</strong>，这个内存也就决定了虚拟机栈所允许的最大深度，在单个线程下，无论是由于<strong>栈帧太大（方法入栈不出栈）或者虚拟机容量太小（最大深度太小）</strong>，当线程内存无法分配时抛出StackOverflowError异常。这里并<strong>没有考虑虚拟机栈动态拓展的特性</strong>。</p></li> <li><p>如果虚拟机在扩展栈时无法申请到足够的内存空间，则抛出<strong>OutOfMemoryError异常</strong>。</p> <p>这里又分为两种情况，一种是对于单线程而言，一种是对于多线程而言。</p> <ul><li><p><strong>在单线程中</strong>，Java虚拟机栈可以动态扩展，并且扩展的动作已经尝试过，但是无法申请到足够的内存去完成扩展</p></li> <li><p><strong>在多线程中</strong>，Java虚拟机新建线程时没有足够的内存去创建对应的虚拟机栈。</p></li></ul></li></ol> <p><strong>虚拟机进程内存 = Xmx（最大堆容量） + MaxPermSize（最大方法区容量）+ 多线程内存（单个线程内存和）</strong></p> <p><strong>线程内存 = Xss（最大虚拟机栈容量） + 最大本地方法栈容量 + 程序计数器（内存很小，忽略）</strong></p> <p><strong>异常提示：</strong> <code>java.lang.StackOverflowError</code> 和 <code>java.lang.OutOfMemoryError</code>。</p> <p><strong>解决方案：</strong></p> <ol><li>栈溢出时，查看异常可以看到问题情况，另外也可以动态拓展。</li> <li>建立在多线程导致的内存溢出时，在不能减少线程数或者更换64位虚拟机的情况下，就只能通过<strong>减少最大堆和减少栈容量来换取更多得线程</strong>。</li></ol> <h3 id="方法区和运行时常量池溢出">方法区和运行时常量池溢出</h3> <p><strong>溢出产生：</strong></p> <ol><li><p>运行时常量池内存溢出，Full GC无法正常回收常量池（1.7版本后不会出现这种情况）。</p></li> <li><p>类无法回收，方法去内存溢出。</p></li></ol> <p>方法区溢出是一个很常见的情况，<strong>一个类要被垃圾收集器回收掉，判定条件是比较苛刻的</strong>。</p> <h3 id="本机直接内存溢出">本机直接内存溢出</h3> <p><strong>溢出产生：</strong> 本机无法分配内存给直接内存时报错内存溢出。</p> <p>DirectMemory容量通过<code>-XX:MaxDirectMemorySize</code>指定，如果不指定，则与Java堆最大值（<code>-Xmx</code>）一样。</p> <p>真正分配直接内存的方法是<code>unsafe.allocateMemory()</code>。</p> <p><strong>异常提示：</strong> 由DirectMemory导致的内存溢出，一个明显的特征是在Heap Dump文件中不会看见明显的异常。OOM之后发现Dump转储快照文件很小，就有可能是这个原因。</p></div> <footer class="page-edit" style="display:none;"><!----> <div class="last-updated"><span class="prefix">Last Updated: </span> <span class="time">2020/12/14 下午10:07:38</span></div></footer> <!----> <!----> <!----></main> <!----></div></div></div></div><div class="global-ui"><div class="back-to-ceiling" style="right:1rem;bottom:6rem;width:2.5rem;height:2.5rem;border-radius:.25rem;line-height:2.5rem;display:none;" data-v-c8d125c6 data-v-c8d125c6><svg t="1574745035067" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5404" class="icon" data-v-c8d125c6><path d="M526.60727968 10.90185116a27.675 27.675 0 0 0-29.21455937 0c-131.36607665 82.28402758-218.69155461 228.01873535-218.69155402 394.07834331a462.20625001 462.20625001 0 0 0 5.36959153 69.94390903c1.00431239 6.55289093-0.34802892 13.13561351-3.76865779 18.80351572-32.63518765 54.11355614-51.75690182 118.55860487-51.7569018 187.94566865a371.06718723 371.06718723 0 0 0 11.50484808 91.98906777c6.53300375 25.50556257 41.68394495 28.14064038 52.69160883 4.22606766 17.37162448-37.73630017 42.14135425-72.50938081 72.80769204-103.21549295 2.18761121 3.04276886 4.15646224 6.24463696 6.40373557 9.22774369a1871.4375 1871.4375 0 0 0 140.04691725 5.34970492 1866.36093723 1866.36093723 0 0 0 140.04691723-5.34970492c2.24727335-2.98310674 4.21612437-6.18497483 6.3937923-9.2178004 30.66633723 30.70611158 55.4360664 65.4791928 72.80769147 103.21549355 11.00766384 23.91457269 46.15860503 21.27949489 52.69160879-4.22606768a371.15156223 371.15156223 0 0 0 11.514792-91.99901164c0-69.36717486-19.13165746-133.82216804-51.75690182-187.92578088-3.42062944-5.66790279-4.76302748-12.26056868-3.76865837-18.80351632a462.20625001 462.20625001 0 0 0 5.36959269-69.943909c-0.00994388-166.08943902-87.32547796-311.81420293-218.6915546-394.09823051zM605.93803103 357.87693858a93.93749974 93.93749974 0 1 1-187.89594924 6.1e-7 93.93749974 93.93749974 0 0 1 187.89594924-6.1e-7z" p-id="5405" data-v-c8d125c6></path><path d="M429.50777625 765.63860547C429.50777625 803.39355007 466.44236686 1000.39046097 512.00932183 1000.39046097c45.56695499 0 82.4922232-197.00623328 82.5015456-234.7518555 0-37.75494459-36.9345906-68.35043303-82.4922232-68.34111062-45.57627738-0.00932239-82.52019037 30.59548842-82.51086798 68.34111062z" p-id="5406" data-v-c8d125c6></path></svg></div><APlayer audio="" fixed="true" mini="true" theme="#b7daff" loop="loop" order="random" preload="auto" volume="0.08" mutex="true" lrc-type="0" list-folded="true" list-max-height="250" storage-name="vuepress-plugin-meting" id="aplayer-fixed"></APlayer></div></div>
    <script src="/assets/js/app.0e76bb72.js" defer></script><script src="/assets/js/3.c5829877.js" defer></script><script src="/assets/js/1.695f3f80.js" defer></script><script src="/assets/js/11.5d72653a.js" defer></script>
  </body>
</html>
